package org.xhnews.ui;

import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FocusChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.decor.BackgroundFactory;

public class FunctionItemField extends LabelField{
	public final static int COLD_STATE = 0;
	public final static int HOT_STATE = 1;
	private int state=COLD_STATE;
	private ShowWindow sw;	
	private WindowFrame wf;
	final FunctionItemField theItem;
	Thread _timerJob;
	public ShowWindow getSw() {
		return sw;
	}

	public void setSw(ShowWindow sw) {
		this.sw = sw;
	}
	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public FunctionItemField(Object text, ShowWindow sw,WindowFrame wf) {
		super(text, LabelField.FOCUSABLE);
		this.sw=sw;
		this.wf=wf;
		theItem=this;
		setFocusListener((FocusChangeListener)wf);
	}

	protected void paint(Graphics graphics) {
		graphics.setColor(Color.WHITE);
		super.paint(graphics);
	}

	public void doChange(){
		switch(this.state){
		case COLD_STATE:
			setBackground(BackgroundFactory
					.createSolidBackground(Color.BLACK));
			break;
		case HOT_STATE:
			wf.updateWindow((Field)sw);	
			setBackground(BackgroundFactory
					.createSolidBackground(Color.GRAY));
			_timerJob=new Thread(new TimerThread(1));
			_timerJob.start();
		    break;			
		}		
	}

	class TimerThread extends Thread {
		int second;
		int delta;

		TimerThread(int second) {
			this.second = second;
			delta = second * 10;
		}

		public void run() {
			int i = 0;
			while ((theItem.isFocus() || theItem.sw.isFocus()) && i < delta) {
				try {
					sleep(100);
					i++;
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			if (i == delta) {

				theItem.sw.reload();
			}
		}
	}

	
	
}
